package com.realestate.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.realestate.constants.AppConstant;
import com.realestate.model.BrtbCategory;
import com.realestate.model.BrtbDistrict;
import com.realestate.model.BrtbProduct;
import com.realestate.model.BrtbProductAttributeValue;
import com.realestate.services.ProductQueryService;

@Controller
public class InitController {
	public static class BrtbProductModel implements Serializable {
		private long id;

		private String description;

		private Date makeDate;

		private String productCode;

		private String productName;

		private String recordStatus;

		private BrtbCategory brtbCategory;

		private BrtbDistrict brtbDistrict;

		private String makerId;
		/**
		 * 
		 */
		private static final long serialVersionUID = -3860310357491380576L;

		public void putAttribute(String key, String value) {
			if (key.compareTo("bedroom") == 0) {
				this.bedroom = value;
			}
			if (key.compareTo("SQRT") == 0) {
				this.SQRT = value;
			}
			if (key.compareTo("room") == 0) {
				this.room = value;
			}
			if (key.compareTo("bathroom") == 0) {
				this.bathroom = value;
			}
			if (key.compareTo("price") == 0) {
				this.price = value;
			}
			if (key.compareTo("address") == 0) {
				this.address = value;
			}
			if (key.compareTo("picture") == 0) {
				this.picture = value;
			}
			if (key.compareTo("special") == 0) {
				this.special = value;
			}
			if (key.compareTo("toilet") == 0) {
				this.toilet = value;
			}
		}

		private String bedroom = "";
		private String SQRT = "";
		private String room = "";
		private String bathroom = "";
		private String price = "";
		private String address = "";
		private String picture = "";
		private String special = "";
		private String toilet = "";

		public String getBedroom() {
			return bedroom;
		}

		public void setBedroom(String bedroom) {
			this.bedroom = bedroom;
		}

		public String getSQRT() {
			return SQRT;
		}

		public void setSQRT(String sQRT) {
			SQRT = sQRT;
		}

		public String getRoom() {
			return room;
		}

		public void setRoom(String room) {
			this.room = room;
		}

		public String getBathroom() {
			return bathroom;
		}

		public void setBathroom(String bathroom) {
			this.bathroom = bathroom;
		}

		public String getPrice() {
			return price;
		}

		public void setPrice(String price) {
			this.price = price;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

		public String getPicture() {
			return picture;
		}

		public void setPicture(String picture) {
			this.picture = picture;
		}

		public String getSpecial() {
			return special;
		}

		public void setSpecial(String special) {
			this.special = special;
		}

		public String getToilet() {
			return toilet;
		}

		public void setToilet(String toilet) {
			this.toilet = toilet;
		}

		public static long getSerialversionuid() {
			return serialVersionUID;
		}

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public Date getMakeDate() {
			return makeDate;
		}

		public void setMakeDate(Date makeDate) {
			this.makeDate = makeDate;
		}

		public String getProductCode() {
			return productCode;
		}

		public void setProductCode(String productCode) {
			this.productCode = productCode;
		}

		public String getProductName() {
			return productName;
		}

		public void setProductName(String productName) {
			this.productName = productName;
		}

		public String getRecordStatus() {
			return recordStatus;
		}

		public void setRecordStatus(String recordStatus) {
			this.recordStatus = recordStatus;
		}

		public BrtbCategory getBrtbCategory() {
			return brtbCategory;
		}

		public void setBrtbCategory(BrtbCategory brtbCategory) {
			this.brtbCategory = brtbCategory;
		}

		public BrtbDistrict getBrtbDistrict() {
			return brtbDistrict;
		}

		public void setBrtbDistrict(BrtbDistrict brtbDistrict) {
			this.brtbDistrict = brtbDistrict;
		}

		public String getMakerId() {
			return makerId;
		}

		public void setMakerId(String makerId) {
			this.makerId = makerId;
		}
	}

	@Autowired
	private ProductQueryService productService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView getdata() {

		List<BrtbProduct> listNewProperty = productService
				.getListproductByCategoryCode(AppConstant.ALL,
						AppConstant.LIMIT);
		List<BrtbProduct> listApartments = productService
				.getListproductByCategoryCode(AppConstant.APARTMENT,
						AppConstant.LIMIT);
		List<BrtbProduct> listHouses = productService
				.getListproductByCategoryCode(AppConstant.HOUSE,
						AppConstant.LIMIT);
		List<BrtbProduct> listVillas = productService
				.getListproductByCategoryCode(AppConstant.VILLA,
						AppConstant.LIMIT);
		List<BrtbProduct> listSApartments = productService
				.getListproductByCategoryCode(AppConstant.SERVICED_APARTMENT,
						AppConstant.LIMIT);
		List<BrtbProduct> listResorts = productService
				.getListproductByCategoryCode(AppConstant.RESORT,
						AppConstant.LIMIT);

		ModelAndView model = new ModelAndView("index");

		model.addObject("listNewProperty", listNewProperty);
		model.addObject("listApartments", listApartments);
		model.addObject("listHouses", listHouses);
		model.addObject("listVillas", listVillas);
		model.addObject("listSApartments", listSApartments);
		model.addObject("listResorts", listResorts);

		return model;

	}

	@RequestMapping("/resultProperty")
	public ModelAndView resultProperty() {
		return new ModelAndView("resultProperty");
	}

	@RequestMapping("/productDetail")
	public ModelAndView productDetail() {
		return new ModelAndView("productDetail");
	}

	@RequestMapping("/advancedSearch")
	public ModelAndView advancedSearch() {
		return new ModelAndView("advancedSearch");
	}

	@RequestMapping("/compareProducts")
	public ModelAndView compareProducts() {
		return new ModelAndView("compareProducts");
	}

	@RequestMapping("/ajaxSearch")
	public ModelAndView ajaxSearch(
			@RequestParam(value = "bedroom", required = false) String bedroom,
			@RequestParam(value = "SQRT", required = false) String SQRT,
			@RequestParam(value = "room", required = false) String room,
			@RequestParam(value = "bathroom", required = false) String bathroom,
			@RequestParam(value = "price", required = false) String price,
			@RequestParam(value = "address", required = false) String address,
			@RequestParam(value = "picture", required = false) String picture,
			@RequestParam(value = "special", required = false) String special,
			@RequestParam(value = "toilet", required = false) String toilet) {
		Map<String, String> mapFilter = new HashMap<String, String>();
		if (bedroom != null && bedroom.length() > 0) {
			mapFilter.put("bedroom", bedroom);
		}
		if (SQRT != null && SQRT.length() > 0) {
			mapFilter.put("SQRT", SQRT);
		}
		if (room != null && room.length() > 0) {
			mapFilter.put("room", room);
		}
		if (bathroom != null && bathroom.length() > 0) {
			mapFilter.put("bathroom", bathroom);
		}
		if (price != null && price.length() > 0) {
			mapFilter.put("price", price);
		}
		if (address != null && address.length() > 0) {
			mapFilter.put("address", address);
		}
		if (picture != null && picture.length() > 0) {
			mapFilter.put("picture", picture);
		}
		if (special != null && special.length() > 0) {
			mapFilter.put("special", special);
		}
		if (toilet != null && toilet.length() > 0) {
			mapFilter.put("toilet", toilet);
		}

		List<BrtbProduct> listResorts = productService
				.getListproductByCategoryCode(AppConstant.ALL, mapFilter,
						AppConstant.LIMIT);
		List<BrtbProductModel> listResult = new ArrayList<InitController.BrtbProductModel>();
		for (BrtbProduct product : listResorts) {
			BrtbProductModel model = new BrtbProductModel();
			model.setId(product.getId());
			model.setDescription(product.getDescription());
			model.setMakeDate(product.getMakeDate());
			model.setProductCode(product.getProductCode());
			model.setProductName(product.getProductName());
			model.setRecordStatus(product.getRecordStatus());
			model.setBrtbCategory(product.getBrtbCategory());
			model.setBrtbDistrict(product.getBrtbDistrict());
			model.setMakerId(product.getMakerId());
			for (BrtbProductAttributeValue attribute : product
					.getBrtbProductAttributeValues()) {
				model.putAttribute(attribute.getBrtbAttribute()
						.getAttributeCode(), attribute.getStrValue());
			}
			listResult.add(model);
		}
		ModelAndView model = new ModelAndView("ajaxSearchResult");
		model.addObject("searchResult", listResult);
		return model;
	}
}
